﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;

public partial class GetWebPageData : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //HttpWebRequest  myHttpWebRequest = (WebRequest.Create("http://127.0.0.1/Care/OutWeb1/Handler.ashx?ID=A101023456") as HttpWebRequest);
        HttpWebRequest myHttpWebRequest = (WebRequest.Create("http://www.google.com.tw") as HttpWebRequest);
        myHttpWebRequest.Method = "POST";
        StreamWriter mgStreamWriter = new StreamWriter(myHttpWebRequest.GetRequestStream(), System.Text.Encoding.GetEncoding("Big5"));

        TextReader tr  = new StreamReader(@"C:\id.xml");

        mgStreamWriter.Write(tr.ReadToEnd());
        mgStreamWriter.Close();
        tr.Close();

        HttpWebResponse myResp = (myHttpWebRequest.GetResponse() as HttpWebResponse);

        StreamReader sr = new StreamReader(myResp.GetResponseStream());
        TextBox2.Text = sr.ReadToEnd();

        sr.Close();
    }

    /// <summary>
    /// 抓URL的HTML碼
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    public string dopost(string url)
    {
        string html = "";
        HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;

        req.Proxy = WebProxy.GetDefaultProxy();
        req.Proxy.Credentials = new NetworkCredential("chi", "chi", "172.16.1.254:3128"); 

        HttpWebResponse res = req.GetResponse() as HttpWebResponse;
        Stream st = res.GetResponseStream();
        StreamReader sr = new StreamReader(st, System.Text.Encoding.Default);

        html = sr.ReadToEnd();

        sr.Close();
        sr.Dispose();
        st.Flush();
        st.Close();
        st.Dispose();
        return html;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBox2.Text=dopost(TextBox1.Text);
    }
}
